let selectSchoolName = "";
let selectSchoolId = "";
let selectCampusName = "";
let selectCampusId = "";
let selectDormName = "";
let selectDormId = "";
// 获取学校信息
function getSchoolList() {
    console.log("getSchoolList");
    $.ajax({
        url: basePath + "/school/getSchoolList",
        contentType: "application/json;charset=UTF-8",
        type: "get",
        async: false, // 设置 AJAX 请求为同步
        dataType: "json",
        complete: function () {
            layer.closeAll('loading');
        },
        success: function (response) {
            var data = response.data;
            var select = $('#schoolSelect');
            select.empty();  // 清空现有选项

            // 遍历数据，为每个学校创建一个 option 元素
            $.each(data, function(index, item) {
                select.append($('<option>', {
                    value: item.id,
                    text: item.name
                }));
            });

            // 选中第一个 option 元素
            select.find('option:first').attr('selected', 'selected');
            selectSchoolId = data[0].id;
            selectSchoolName = data[0].name;
            console.log("getSchoolList");
            console.log(data);
            // 更新 Layui 的渲染
            layui.form.render('select');
            getCampusList(selectSchoolId);
        }, error: function () {
            console.log("error")
        }
    });
}

// 获取校区信息
function getCampusList(schoolId) {
    console.log("进来了")
    console.log(schoolId)
    $.ajax({
        url: basePath + "/school/getCampusListBySchoolId/" + schoolId,
        contentType: "application/json;charset=UTF-8",
        type: "get",
        dataType: "json",
        complete: function () {
            layer.closeAll('loading');
        },
        success: function (response) {
            var data = response.data;
            var select = $('#campusSelect');
            select.empty();  // 清空现有选项

            // 遍历数据，为每个校区创建一个 option 元素
            $.each(data, function(index, item) {
                select.append($('<option>', {
                    value: item.id,
                    text: item.name
                }));
            });

            // 选中第一个 option 元素
            select.find('option:first').attr('selected', 'selected');
            selectCampusId = data[0].id;
            selectCampusName = data[0].name;
            console.log("getCampusList");
            console.log(data);
            getDormList(selectCampusId);
            // 更新 Layui 的渲染
            layui.form.render('select');
        }, error: function () {
            console.log("error")
        }
    });
}


// 获取宿舍信息
function getDormList(campusId) {
    $.ajax({
        url: basePath + "/school/getDormListByCampusId/" + campusId,
        contentType: "application/json;charset=UTF-8",
        type: "get",
        dataType: "json",
        complete: function () {
            layer.closeAll('loading');
        },
        success: function (response) {
            var data = response.data;
            var select = $('#dormSelect');
            select.empty();  // 清空现有选项

            // 遍历数据，为每个校区创建一个 option 元素
            $.each(data, function(index, item) {
                select.append($('<option>', {
                    value: item.id,
                    text: item.name
                }));
            });

            // 选中第一个 option 元素
            select.find('option:first').attr('selected', 'selected');
            selectDormId = data[0].id;
            selectDormName = data[0].name;
            console.log("getDormList");
            console.log(data);
            // 更新 Layui 的渲染
            layui.form.render('select');
        }, error: function () {
            console.log("error")
        }
    });
}
layui.use(['form', 'element', 'util', 'carousel', 'form', 'laypage', 'layer','table','upload'], function () {
    var element = layui.element;
    var util = layui.util;
    var carousel = layui.carousel;
    var form = layui.form;
    var laypage = layui.laypage
        , layer = layui.layer
        , upload=layui.upload;

    // 获取学校信息
    getSchoolList();

    console.log("初始化");

    // 获取选中的菜单
    form.on('select(setSchool)', function (data) {
        var indexGID = data.elem.selectedIndex;
        selectSchoolName = data.elem[indexGID].label;
        selectSchoolId = data.elem[indexGID].value;
        getCampusList(selectSchoolId);
    });

    // 获取选中的菜单
    form.on('select(setCampus)', function (data) {
        var indexGID = data.elem.selectedIndex;
        selectCampusName = data.elem[indexGID].label;
        selectCampusId = data.elem[indexGID].value;
        getDormList(selectCampusId);
    });

    // 获取选中的菜单
    form.on('select(setDorm)', function (data) {
        var indexGID = data.elem.selectedIndex;
        selectDormName = data.elem[indexGID].label;
        selectDormId = data.elem[indexGID].value;
    });

    //普通图片上传
    upload.render({
        elem: '#uimage'
        ,url: basePath+'/user/updateuimg'
        ,before: function(obj){
            layer.load(1, { //icon支持传入0-2
                content: '上传中',
                success: function (layero) {
                    layero.find('.layui-layer-content').css({
                        'padding-top': '39px',
                        'width': '60px'
                    });
                }
            });
        },done: function(res){
            layer.closeAll('loading');
            layer.msg('修改成功', {
                time: 1000,
                icon: 1,
                offset: '150px'
            }, function () {
                location.reload();
            });
        },error: function(){
            layer.msg('上传失败');
        }
    });
    form.on('submit(demo1)', function(data){
        var object = new Object();
        object["username"] = data.field.username;
        object["email"] = data.field.email;
        object["sex"] = data.field.sex;
        object["schoolid"] = selectSchoolId;
        object["schoolname"] = selectSchoolName;
        object["campusid"] = selectCampusId;
        object["campusname"] = selectCampusName;
        object["dormid"] = selectDormId;
        object["dormname"] = selectDormName;
        var jsonData = JSON.stringify(object);
        $.ajax({
            url: basePath + "/user/updateinfo",
            data: jsonData,
            contentType: "application/json;charset=UTF-8",
            type: "post",
            dataType: "json",
            beforeSend: function () {
                layer.load(1, { //icon支持传入0-2
                    content: '修改中...',
                    success: function (layero) {
                        layero.find('.layui-layer-content').css({
                            'padding-top': '39px',
                            'width': '60px'
                        });
                    }
                });
            },
            complete: function () {
                layer.closeAll('loading');
            },
            success: function (data) {
                if (data.status == 200) {
                    layer.msg(data.message, {
                        time: 1000,
                        icon: 1,
                        offset: '100px'
                    }, function () {
                        location.reload();
                    });
                } else {
                    layer.msg(data.message, {
                        time: 1000,
                        icon: 2,
                        offset: '100px'
                    });
                }
            },error:function () {
                layer.msg('系统异常');
            }
        });
        return false;
    });
});